package com.wuzf.mall.net

import com.wuzf.mall.config.Configuration
import com.wuzf.mall.global.GlobalKeys
import com.wuzf.mall.global.Mall
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.util.concurrent.TimeUnit


object RestCreator {

    private object HttpClientHolder {
        val client: OkHttpClient = OkHttpClient
            .Builder()
            .connectTimeout(60L, TimeUnit.SECONDS)
            .build()
    }


    private object RestCreatorHolder {
        private val baseUrl = Mall.getConfiguration<String>(GlobalKeys.API_HOST)
        val retrofitClient: Retrofit = Retrofit.Builder()
            .baseUrl(baseUrl)
            .client(HttpClientHolder.client)
            // 设置字符串转换的工具，同样也可以使用gson
            .addConverterFactory(ScalarsConverterFactory.create())
            .build()
    }

    private object RestServiceHolder {
        val restService: RestService =
            RestCreatorHolder.retrofitClient.create(RestService::class.java)
    }

    val restService: RestService
        get() = RestServiceHolder.restService
}